package Controller;

import Model.*;
//import Model.ServorgInfoType;

import Service.kkfwService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("statistics")

public class kkfwController {
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Resource
    private kkfwService service;

    @RequestMapping("kkfw")
    @ResponseBody
    public List<jdfw> kkfw() {
        return service.list();
    }

    //服务老人数
    @RequestMapping("eldercount")
    @ResponseBody
    public int eldercount() {
        return service.eldercount();
    }

    //在职员工数 护工
    @RequestMapping("staffcount")
    @ResponseBody
    public int staffcount() {
        return service.staffcount();
    }

    //服务机构数
    @RequestMapping("jdfwcount")
    @ResponseBody
    public int jdfwcount() {
        return service.jdfwcount();
    }

    //本月和上月老人或护工的数量
    @RequestMapping("contrast")
    @ResponseBody
    public jdfw contrast() {
        return service.contrast();
    }

    //获取每月老人数量
    @RequestMapping("bymonth")
    @ResponseBody
    public String bymonth() {
        return service.bymonth();
    }

    //签到签退滚动模块

    @RequestMapping("sign")
    @ResponseBody
    public List<kfkw> sign(String state) {
        return service.listsing(state);
    }

    //定时任务
    @Scheduled(cron = "0 0 1 1 * ?")
//    @Scheduled(cron = "*/5 * * * * ?")
    public void create() {
        service.listStaffmonth();
        //System.out.println("定时任务一已执行!");
    }


    //查询护工最近半年的统计信息

    @RequestMapping("listStaffmonth6")
    @ResponseBody
    public List<kfkw> listStaffmonth6() {
        return service.listStaffmonth6();

    }



    //查询老人最近半年的统计信息
    @RequestMapping("listEldermonth6")
    @ResponseBody
    public List<kfkw> listEldermonth6() {
        return service.listEldermonth6();
    }

    //根据时间段查询老人的统计信息
    @RequestMapping("selectEldertime")
    @ResponseBody
    public  List<kfkw> selectEldertime(@RequestParam(value = "timeStart",required = false)String timeStart, @RequestParam(value = "timeEnd",required = false)String timeEnd) {
        List<kfkw> kfkw = null;
        try {
            System.out.println("老人时间段列表查询try" + kfkw);
            kfkw = service.selectEldertime(timeStart,timeEnd);
            System.out.println("老人时间段列表查询" + kfkw);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("老人时间段列表：" + kfkw);
        return kfkw;
    }

    //根据时间段以及机构查询被护理老人的统计信息
    @RequestMapping("selectByElder")
    @ResponseBody
    public  List<kfkw> selectByElder(@RequestParam(value = "timeStart",required = false)String timeStart, @RequestParam(value = "timeEnd",required = false)String timeEnd, @RequestParam(value = "getServorg",required = false)String getServorg) {
//        if (yearSearch==null||yearSearch==""||yearSearch.equals(null)||yearSearch.equals("")){
//            Calendar cal = Calendar.getInstance();
//            int years = cal.get(Calendar.YEAR);
//            yearSearch=years+"%";
//        }else{
//            yearSearch=yearSearch+"%";
//            yearSearch=yearSearch+"%";
//        }
        System.out.println("选择的时间段："+timeStart+timeEnd);
        System.out.println("操作机构"+ getServorg);
        List<kfkw> selectByElder = null;
        try {
            System.out.println("被护理老人统计列表查询before" + JSON.toJSONString(selectByElder));
            selectByElder = service.selectByElder(timeStart,timeEnd,getServorg);
            System.out.println("被护理老人统计列表查询after" + JSON.toJSONString(selectByElder));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("被护理老人统计列表汇总：" + JSON.toJSONString(selectByElder));
        return selectByElder;
    }


    //统计合计fuwushu
    @RequestMapping("fuwushuSum")
    @ResponseBody
    public List<ElderStatisticSum> fuwushuSum(@RequestParam(value = "timeStart",required = false)String timeStart, @RequestParam(value = "timeEnd",required = false)String timeEnd,@RequestParam(value = "getServorg",required = false)String getServorg){
        System.out.println("~~~~~~~~~");
        List<ElderStatisticSum> list = service.fuwushuSum(timeStart,timeEnd,getServorg);
        System.out.println("fuwushuSum返回list长度"+list.size());
        System.out.println("fuwushuSum查询结果为：" +list);
        return list;
    }


    //查询护工某年的统计信息
    @RequestMapping("listStaffyear")
    @ResponseBody
    public List<kfkw> listStaffyear(String year) {

        return service.listStaffyear(year);
    }

    //查询老人某年的统计信息
    @RequestMapping("listElderyear")
    @ResponseBody
    public List<kfkw> listElderyear(String year) {

        return service.listElderyear(year);
    }

    //查询机构种类,动态下拉框用的
    @RequestMapping("getServorg")
    @ResponseBody

    public List<kfkw> getServorg() throws Exception{
        List<kfkw> getServorg = null;

        try {
            getServorg = service.getServorg();
            System.out.println("机构列表查询=getServorg:" + getServorg);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("机构列表查询异常！", e);
        }
        System.out.println("机构列表：" + JSON.toJSONString(getServorg));
        return getServorg;
    }



    //根据时间段以及机构查询护工的统计信息
    @RequestMapping("selectByhg")
    @ResponseBody
    public  List<kfkw> selectByhg(@RequestParam(value = "yearSearch",required = false)String yearSearch, @RequestParam(value = "getServorg",required = false)String getServorg) {
        if (yearSearch==null||yearSearch==""||yearSearch.equals(null)||yearSearch.equals("")){
            Calendar cal = Calendar.getInstance();
            int years = cal.get(Calendar.YEAR);
            yearSearch=years+"%";
        }else{
            yearSearch=yearSearch+"%";
            yearSearch=yearSearch+"%";
        }

        System.out.println("选择的年份："+yearSearch);
        System.out.println("操作机构"+ getServorg);
        List<kfkw> selectByhg = null;
        try {
            System.out.println("护工统计列表查询before" + JSON.toJSONString(selectByhg));
            selectByhg = service.selectByhg(yearSearch,getServorg);
            System.out.println("护工统计列表查询after" + JSON.toJSONString(selectByhg));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("护工统计列表汇总：" + JSON.toJSONString(selectByhg));
        return selectByhg;
    }




    //得到日志表格
    @RequestMapping(value = "/getAllSelectedList",produces ={"application/json;charset=UTF-8"})
    @ResponseBody
    public PageInfo getAllSelectedList(@RequestParam(value = "page",defaultValue = "1")Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        PageHelper.startPage(page, limit);
        List<kfkw> getAllSelectedList = service.getAllSelectedList();
        PageInfo pdr = new PageInfo(getAllSelectedList);
        return pdr;
    }

}
